package com.itheima.mynetty.handler.server;

import com.itheima.netty.pojo.UserInfo;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ServerInboundHandler1 extends ChannelInboundHandlerAdapter {

    int count = 1;

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("ServerInboundHandler1 channelActive-----");
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        log.info("ServerInboundHandler1 channelInactive-----");
        super.channelInactive(ctx);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
//        log.info("ServerInboundHandler1 channelRead-----");

//        ByteBuf buf = (ByteBuf) msg;
//        byte[] bytes = new byte[buf.readableBytes()];
//        buf.readBytes(bytes);
//        String msg1 = new String(bytes, CharsetUtil.UTF_8);
//        log.info("---服务端收到的第{}个数据:{}", count++, msg1);

//        String msg1 = (String) msg;
//        log.info("---服务端收到的第{}个数据:{}", count++, msg1);

//        MessageProto.Message message = (MessageProto.Message) msg;
//        log.info("---服务端收到的第{}个数据:{}", count++, message.getContent());

        UserInfo userInfo = (UserInfo) msg;
        log.info("---服务端收到的第{}个数据:{}", count++, userInfo);

//
//        byte[] bytes1 = "你好client, 我是server".getBytes(CharsetUtil.UTF_8);
//        ByteBuf buffer = ctx.alloc().buffer(bytes1.length);
//        buffer.writeBytes(bytes1);
//        ctx.writeAndFlush(buffer);

        super.channelRead(ctx, msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        log.info("ServerInboundHandler1 channelReadComplete-----");
        super.channelReadComplete(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        log.info("ServerInboundHandler1 exceptionCaught-----");
        super.exceptionCaught(ctx, cause);
    }
}
